/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.display {
	import com.googlecode.milib.base.MLBaseClass;
	import com.googlecode.milib.display.service.MLMC;

	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.utils.Dictionary;

	/**
	 * @author Marek Brun
	 */
	public class IndexKeeper extends MLBaseClass {
		
		private static const servicedObjects : Dictionary = new Dictionary(true);
		private var mlmc:MLMC;
		private var dictChildAndItem:Dictionary;
		
		public function IndexKeeper(access:Private, mc:MovieClip) {
			mlmc=MLMC.forInstance(mc);
			dictChildAndItem=new Dictionary(true);
		}

		public function get mc():MovieClip { return mlmc.mc; }
		
		public function addChildToKeepIndex(child:DisplayObject):void {
			if(!child.parent){
				throw new ArgumentError('Passed child ('+child.name+') must be added to '+mc.name);
			}
			if(child.parent!=mc){
				throw new ArgumentError('Passed child ('+child.name+') must have '+mc.name+' MovieClip as parent');
			}
			dictChildAndItem[child]=new Item(child);
		}
		
		
		static public function forInstance(mc:MovieClip):IndexKeeper {
			if(servicedObjects[mc]){
				return servicedObjects[mc];
			}else{
				servicedObjects[mc]=new IndexKeeper(null, mc);
				return servicedObjects[mc];
			}
		}
	}
}

import com.googlecode.milib.base.MLBaseClass;

import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.events.Event;

internal class Private {
}

internal class Item extends MLBaseClass {

	private var mc:DisplayObject;
	private var down:Array;
	private var up:Array;
	private var parent:DisplayObjectContainer;

	public function Item(mc:DisplayObject) {
		this.mc=mc;		up=[];
		var i:int;
		var index:uint=mc.parent.getChildIndex(mc);
		for(i=index+1;i<mc.parent.numChildren;i++){
			up.push(mc.parent.getChildAt(i));
		}
		down=[];
		for(i=index-1;i>-1;i--) {
			down.push(mc.parent.getChildAt(i));
		}
		
		mc.addEventListener(Event.ADDED_TO_STAGE, onMCADDED_TO_STAGE, false, 0, true);
		parent=mc.parent;
	}
	
//********************************************************************************************
//		events for Item
//********************************************************************************************
		protected function onMCADDED_TO_STAGE(event:Event):void {
			mc.removeEventListener(Event.ADDED_TO_STAGE, onMCADDED_TO_STAGE);
			parent.removeChild(mc);
			var i:uint;
			var loop:DisplayObject;
			for(i=0;i<up.length;i++){
				loop=up[i];
				if(loop.parent){
					parent.addChildAt(mc, parent.getChildIndex(loop));
					//dbg. log('add '+mc.name+' under '+loop.name);
					break;
				}
			}
			if(!mc.parent){
				for(i=0;i<down.length;i++){
					loop=down[i];
					if(loop.parent){
						parent.addChildAt(mc, parent.getChildIndex(loop)+1);
						//dbg. log('add '+mc.name+' over '+loop.name);
						break;
					}
				}
			}
			if(!mc.parent) {
				parent.addChild(mc);
			}
			mc.addEventListener(Event.ADDED_TO_STAGE, onMCADDED_TO_STAGE, false, 0, true);
		}
}









